package com.honeycomb.action.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import com.honeycomb.action.BaseAjaxAction;
import com.honeycomb.service.IUserService;
import com.honeycomb.utils.SystemConstants;
import com.honeycomb.vo.BaseVO;
import com.honeycomb.vo.ResultDto;
import com.honeycomb.vo.UserInfoVO;

/**
 * @author ZhongJiang
 * 
 * @function AJAX更新用户基本信息
 * 
 * @param userId 用户名
 * 
 * @param userNickname 用户昵称
 * 
 * @param userSex 用户性别
 * 
 * @param ... 更多
 * 
 * @return json
 */
public class AjaxUpdateUserInfoAction extends BaseAjaxAction {

	private IUserService iUserService;
	
	private String userId;
	private String userNickname;
	private String userSex;
	
	public IUserService getiUserService() {
		return iUserService;
	}
	public void setiUserService(IUserService iUserService) {
		this.iUserService = iUserService;
	}
	public String getUserId() {
		return userId;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}
	public String getUserNickname() {
		return userNickname;
	}
	public void setUserNickname(String userNickname) {
		this.userNickname = userNickname;
	}
	public String getUserSex() {
		return userSex;
	}
	public void setUserSex(String userSex) {
		this.userSex = userSex;
	}
	
	@Override
	protected JSONObject doRequest(HttpServletRequest request,
			HttpServletResponse response, HttpSession session) throws Exception {
		logger.info("-------------------AjaxUpdateUserInfoAction-----------------");
		
		UserInfoVO userInfoVO = new UserInfoVO();
		ResultDto<BaseVO> resultDtoUpdateUserInfo = new ResultDto<BaseVO>();
		JSONObject jo = new JSONObject();
		try {
			userInfoVO.setUserId(this.getUserId());
			userInfoVO.setUserNickname(this.getUserNickname());
			userInfoVO.setUserSex(this.getUserSex());
			resultDtoUpdateUserInfo = this.iUserService.doUpdateUserInfo(userInfoVO);
			if(resultDtoUpdateUserInfo.isSuccess()) {
				//更新session里的UserInfoVO
				UserInfoVO sessionUserInfo = (UserInfoVO)session.getAttribute(SystemConstants.HoneyComb_LoginUser_Session_Name);
				sessionUserInfo.setUserNickname(this.getUserNickname());
				sessionUserInfo.setUserSex(this.getUserSex().substring(0, 1));
				session.setAttribute(SystemConstants.HoneyComb_LoginUser_Session_Name, sessionUserInfo);
				jo.put("result", SystemConstants.AJAX_SUCCESS);
			}else {
				jo.put("result", SystemConstants.AJAX_FAILED_ALERT);
			}
			return jo;
		} catch (Exception e) {
			e.printStackTrace();
			jo.put("result", SystemConstants.AJAX_FAILED_ALERT);
			return jo;
		}
	}

}
